/**
 * @Author: guo-bomin 2974463764@qq.com
 * @Date: 2024-02-27 11:17:37
 * @LastEditors: guo-bomin 2974463764@qq.com
 * @LastEditTime: 2024-02-27 11:17:47
 * @FilePath: \my-vue-app\src\utils\distances.js
 * @Description: 郭博民16670506200
 * @有问题微信同号询问，急事call我
 * @Copyright (c) 2024 by 湖南習羽网络科技有限公司, All Rights Reserved.
 */
// 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
export function getDistances(lat1, lng1, lat2, lng2) {
  let EARTH_RADIUS = 6378.137; // 地球半径
  let radLat1 = (lat1 * Math.PI) / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
  let radLat2 = (lat2 * Math.PI) / 180.0;
  let a = radLat1 - radLat2;
  let b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  let s =
    2 *
    Math.asin(
      Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))
    );
  s = s * EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; // 输出为公里
  return { m: Math.round(s * 1000), km: Number(s.toFixed(2)) };
}
